<?php
/*------------------------------------------------------------------------------ 
    
    Create Date :2010-08-13
    Author    : Phan Van Dam
    Copyright(c) 2010 A.D.A Solution. All rights reserved
    
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#            Date          Author       Note
    3.0     thiếu comment  2011/03/10     HoangPhuc     Review code,update comment for source
------------------------------------------------------------------------------*/

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('fmfrmstaff_loadGrid');      
$objXajax->registerFunction('fmfrmstaff_save');
$objXajax->registerFunction('fmfrmstaff_edit');
$objXajax->registerFunction('fmfrmstaff_delete');

function fmfrmstaff_save($params)
{
    global $objDbSelect,$user_id,$hotel_id;
    $svt_film = SVT_FILM;
    $objResponse =  new xajaxResponse();  
    $name = addslashes($params['txtname']);
    $alias_name =addslashes($params['txtnamealias']) ;
    if($alias_name==''){
        $alias_name=$name;
    }
    $birthday = addslashes($params['birthdate']) ;
    $cmnd = $params['txtcmnd'];
    $phone = $params['txtphone'];
    $salary = $params['txtsalary'];
    $date_work = $params['datework'];
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql ="UPDATE cus_staff SET name = '$name',alias_name = '$alias_name',birthday = '$birthday',
            cmnd = '$cmnd',phone = '$phone',salary='$salary',date_work='$date_work',
            user_update = '$user_id',date_update = now() WHERE id =$id"; 
                
    }else{
          $sql= " INSERT INTO cus_staff(name,alias_name,birthday,cmnd,phone,salary,date_work,user_created,
          date_created,user_update,date_update,service_type,service_id) VALUES('$name','$alias_name','$birthday',
          '$cmnd','$phone','$salary','$date_work','$user_id',now(),'$user_id',now(),$svt_film,$hotel_id)"; 
           
     } 
     
     if($objDbSelect->Execute($sql)){
       $objResponse->addScript("checkUpdateStatus(1,'fmfrmstaff','$edit');");
       $objResponse->addScript('xajax_fmfrmstaff_loadGrid();'); 
        }else{
       $objResponse->addScript("checkUpdateStatus(0,'fmfrmstaff','$edit');");
    }
    
    $objResponse->addScript("hasChange= true;");
     
/*     $objDbSelect->Execute($sql);   
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_fmfrmstaff_loadGrid();'); */    
    return $objResponse->getXML();
}


function fmfrmstaff_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = fmfrmstaff_getGrid(); 
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/fm_staff_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmstaff_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmstaff_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function fmfrmstaff_getGrid()
{
    global $objDbSelect,$lang,$hotel_id;
    $svttype = SVT_FILM;
    $sqlSelect = " select * from cus_staff WHERE  service_id = $hotel_id and service_type = $svttype  order by name ";                                 
    return $objDbSelect->GetArray($sqlSelect);
}  

    function fmfrmstaff_edit($id){
        global $objDbSelect,$hotel_id;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT name,alias_name,DATE_FORMAT(birthday,'%d/%m/%Y') birthday,salary,DATE_FORMAT(date_work,'%d/%m/%Y') date_work,
        cmnd,phone FROM cus_staff WHERE id = '$id' and service_id ='$hotel_id' ";
        
        $arr = $objDbSelect->GetArray($sql);
       // echo '<pre>'; print_r($arr); die();
 
        $birthday     =substr($arr[0]['birthday'],0,10) ;
        $salary       = $arr[0]['salary'];
        $salary = FormatMoney($salary);       
        $date_work    =substr($arr[0]['date_work'],0,10) ;       
        $cmnd         = $arr[0]['cmnd'];       
        $phone        = $arr[0]['phone'];
        $roomname =htmlspecialchars_decode($arr[0]['name']) ;
        $name_alias =htmlspecialchars_decode($arr[0]['alias_name']) ;
        
        $objResponse->addAssign('fmfrmstaff_txtname','value',$name);       
        $objResponse->addAssign('fmfrmstaff_txtnamealias','value',$name_alias);       
        $objResponse->addScript("$('#fmfrmstaff_birthdate').val('$birthday');");
        $objResponse->addScript("$('#fmfrmstaff_txtcmnd').val('$cmnd');");
        
        $objResponse->addScript("$('#fmfrmstaff_txtphone').val('$phone');");
        $objResponse->addScript("$('#fmfrmstaff_txtsalary').val('$salary');");
        $objResponse->addScript("$('#fmfrmstaff_datework').val('$date_work');");
        
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
?>
